package com.http;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.ChatMsgStruct;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:ChatMsgListTask
 * @Description:TODO 发送消息
 * @author zhuw
 * @date 2013-8-19 上午11:36:48
 */
public class ChatMsgAddTask extends AsyncTask<String, Void, ResultChatMsgObj> {
	public static interface CallBack extends NetWorkCallBack {
		/**
		 * 
		 * @Description:TODO发送消息
		 * @param@param data
		 * @return void
		 * @author zhuw
		 * @date 2013-8-19 下午3:24:00
		 */
		public abstract void procChatMsgAddData(ChatMsgStruct data);
		public abstract void procChatMsgAddFailed(String msg);

	}

	CallBack callback;
	String hisThumb;//对方头像
	String myThumb;//我的头像
	public ChatMsgAddTask(CallBack callback,String hisThumb,String myThumb) {
		super();
		this.callback = callback;
		this.hisThumb = hisThumb;
		this.myThumb = myThumb;
	}

	@Override
	protected ResultChatMsgObj doInBackground(String... params) {
		ResultChatMsgObj data = null;
		try {
			
			ArrayList<NameValuePair> paramPairs=new ArrayList<NameValuePair>();
			paramPairs.add(new BasicNameValuePair("uid", params[0]));
			paramPairs.add(new BasicNameValuePair("fid", params[1]));
			paramPairs.add(new BasicNameValuePair("type", params[2]));
			paramPairs.add(new BasicNameValuePair("content", params[3]));
			ResEntity re=NetConUtil.postByForm(ServerPath.CHATMSG_ADD_PATH,paramPairs);
			if(re.code==ResEntity.S_OK){
			    data= new ResultChatMsgObj(CommonData.H_FLAG_SUCCESS, null, JsonUtil.parseChatMsObj(new JSONObject(re.jsonStr),params[0],hisThumb,myThumb));
			}else if(re.code==ResEntity.S_FAILED){
				
				  data= new ResultChatMsgObj(CommonData.H_FLAG_ERROR, "code:"+re.code+"    "+re.jsonStr, null);
				
			}else{
				  data= new ResultChatMsgObj(CommonData.H_FLAG_ERROR, "code:"+re.code+ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data= new ResultChatMsgObj(CommonData.H_FLAG_ERROR, ErrorCode.ERROR_MSG, null);
			e.printStackTrace();
		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultChatMsgObj result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procChatMsgAddData(result.data);

		} else {
			callback.procChatMsgAddFailed(result.msg);
		}

	}
}
